var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSwagger<MyModule>();
var app = builder.Build();
app.ConfigureSwagger();
app.Run();

public static class SwaggerHelper
{
    public static MyModule _module;

    public static IServiceCollection AddSwagger<TMdule>(this IServiceCollection services)
        where TMdule : MyModule, new()
    {
        _module = new TMdule();
        _module.ConfigureServices(services);
        return services;
    }

    public static WebApplication ConfigureSwagger(this WebApplication app)
    {
        _module.Configure(app);
        app.UseAuthorization();
        app.MapControllers();
        //app.UseHttpsRedirection();
        return app;
    }
}

public class MyModule
{
    public IServiceCollection ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddEndpointsApiExplorer();
        services.AddSwaggerGen();
        return services;
    }

    public WebApplication Configure(WebApplication app)
    {
        if (app.Environment.IsDevelopment())
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }
        return app;
    }
}
